package com.gitee.fsmxm.thinking.in.spring.ioc.overview.container;

import com.gitee.fsmxm.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * {@link org.springframework.context.ApplicationContext} 作为 ioc 容器 demo
 */
public class ApplicationContextAsIocContainerDemo {

    public static void main(String[] args) {

        // 注解配置应用上下文
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 注册配置类
        applicationContext.register(ApplicationContextAsIocContainerDemo.class);

        // 启动应用上下文
        applicationContext.refresh();

        // 依赖查找
        System.out.println(applicationContext.getBean(User.class));

        // 关闭容器
        applicationContext.close();

    }

    @Bean
    User user() {
        User user = new User();
        user.setId(1L);
        user.setName("小马哥");
        return user;
    }

}
